package cqu.edu.cn.interceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * SpringBoot组件配置类，可以创建Bean，这里为了注册拦截器拦截非法登录
 * 配置使用@Configuration注解
 * @author 杨鑫隆and高鸿成
 *
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Bean
    WebInterceptor webInterceptor() {
        return new WebInterceptor();
    }

    /*
     * 注册拦截器，规定哪些访问拦截哪些不拦截
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(this.webInterceptor())
                .addPathPatterns("/**") 
                .excludePathPatterns("/login")
                .excludePathPatterns("/logout") 
                .excludePathPatterns("/sso/login")
                .excludePathPatterns("/sso/logOut")
                .excludePathPatterns("/sso/checkLogin")
                .excludePathPatterns("/sso/checkToken");
    }
    
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/"
    };

    /*
     * 处理对静态资源的访问
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (!registry.hasMappingForPattern("/webjars/**")) {
            registry.addResourceHandler("/webjars/**").addResourceLocations(
                    "classpath:/META-INF/resources/webjars/");
        }
        if (!registry.hasMappingForPattern("/**")) {
            registry.addResourceHandler("/**").addResourceLocations(
                    CLASSPATH_RESOURCE_LOCATIONS);
        }   
    }

    
}
